home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2952 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.2 KB

  1. Path: news.mira.net.au!news
  2. From: davidw@werple.net.au (David White)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [] overload..(newbie in distress)
  5. Date: 21 Jan 1996 09:09:56 +1100
  6. Organization: Werple Internet, Melbourne
  7. Message-ID: <4drp7k$bj1@werple.net.au>
  8. References: <DLGppJ.31C@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: werple.mira.net.au
  10.  
  11. tthiraku@landen.math.uwaterloo.ca (Thanou Thirakul) writes:
  12.  
  13. >Hi..
  14.  
  15. >I'm currently stuck on how to overload [] operator such that
  16. >it does two different tasks..
  17.  
  18.  
  19. >case 1:
  20.  
  21. >// A is an instance of a class that contains a linkist.
  22.  
  23. >   A[5] = val;  // store val into the fifth node of a linklist. 
  24.  
  25. For this you can use a member function such as:
  26.  
  27.     T &operator[](int i);
  28.  
  29. If the linked list needs to know when one of its items has changed, you 
  30. need something more elaborate. This can be done by returning an instance 
  31. of a helper class instead of a T&; the helper object then receives the 
  32. assignment operation and can notify the linked list.
  33.       
  34. >   val = A[5] ; // returns the value of the fifth node of a linklist.  
  35.  
  36. And for this you can use:
  37.  
  38.     T operator[](int i) const;
  39.  
  40. >I was wondering how can C++ differentiate these two senerios? 
  41.  
  42.  
  43. David White
  44. davidw@werple.mira.net.au
  45.